/*
 * Daniel R Padilla
 *
 * Copyright (c) 2008, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.xml;


import java.io.File;
import java.util.ArrayList;

import org.databasefacade.ConnectionProperties;
import org.databasefacade.LogManager;
import org.databasefacade.exception.DatabaseFacadeException;
import org.databasefacade.util.XMLDataRecord;
import org.databasefacade.util.XMLDataRecordList;
import org.databasefacade.util.XMLParseUtil;
import org.w3c.dom.Document;


/**
 *  @Author Daniel
 *
 *  This XMLPropertiesLoader looks for recognizable
 *  tags within an XML file a loads the tags into 
 *  a properties.
 *  
 *  XML Handled by this reader.
 *  
 *  <DatabaseFacadeConnection>
 *      <DatabaseVendor>MYSQL</DatabaseVendor>
 *      <DataSourceClass>
 *           com.mysql........MyDataSource()
 *      </DataSourceClass>
 *      <SetupMethods>
 *         <Method name="setUrl" param="xxxx"/>
 *      <SetupMethods>
 *  </DatabaseFacadeConnection>
 *  
 */
public class XMLConnectionPropertiesLoader {

    public ConnectionProperties load(File xmlFile) 
        throws DatabaseFacadeException {
        return parse(xmlFile);
    }
 
    // Parse the XML Document
    private ConnectionProperties parse(File file) 
        throws DatabaseFacadeException {
    	ConnectionProperties props = new ConnectionProperties();
        XMLParseUtil xmlutil = new XMLParseUtil();
        Document doc = xmlutil.getDocument(file);
        XMLDataRecordList data = new XMLDataRecordList();
        ArrayList tagHierarchy = new ArrayList();
    	   	
        // Get Vendor Information
        tagHierarchy = new ArrayList();
        tagHierarchy.add("DatabaseFacadeConnection");
        tagHierarchy.add("DatabaseVendor");
        data = new XMLDataRecordList();
        xmlutil.assignTagXMLData(data, tagHierarchy, doc);
        LogManager.info(data.toString());
        if ( data.size() == 1 ) {
        	XMLDataRecord rec = data.get(0);
        	props.setDatabaseType(rec.getValue());
        }
    	
        // Get DataSource Class Information
        tagHierarchy = new ArrayList();
        tagHierarchy.add("DatabaseFacadeConnection");
        tagHierarchy.add("DataSourceClass");
        data = new XMLDataRecordList();
        xmlutil.assignTagXMLData(data, tagHierarchy, doc);
        LogManager.info(data.toString());
        if ( data.size() == 1 ) {
        	XMLDataRecord rec = data.get(0);
        	props.setDataSourceClass(rec.getValue());
        }
    	
        // Get initialization methods information
        tagHierarchy = new ArrayList();
        tagHierarchy.add("DatabaseFacadeConnection");
        tagHierarchy.add("SetupMethods");
        tagHierarchy.add("Method");
        data = new XMLDataRecordList();
        xmlutil.assignTagXMLData(data, tagHierarchy, doc);
        LogManager.info(data.toString());
        for (int i=0; i < data.size(); i++) {
        	XMLDataRecord rec = data.get(i);
        	String methodName = rec.getAttributeValue("name");
        	String parameter = rec.getAttributeValue("param");
        	props.addMethod(methodName, parameter);
        }
        
        // Get Database Meta-Data Information
        tagHierarchy = new ArrayList();
        tagHierarchy.add("DatabaseFacadeConnection");
        tagHierarchy.add("DatabaseMetaData");
        data = new XMLDataRecordList();
        xmlutil.assignTagXMLData(data, tagHierarchy, doc);
        LogManager.info(data.toString());
        if ( data.size() == 1 ) {
        	XMLDataRecord rec = data.get(0);
        	props.setMetaDataDir(rec.getValue());
        }
        return props;
    }
    

}
